<template>
  <el-card shadow="never">
    <div slot="header">添加问卷
      <el-button
        type="info"
        icon="el-icon-back"
        size="mini"
        @click="$router.push({name:'papers'})"
        plain
      >返回</el-button>
    </div>
    <el-form :model="form" ref="form" label-width="80px">
      <el-form-item label="标题">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="班级">
        <el-select v-model="form.class_id" placeholder="请选择">
          <el-option
            v-for="cls in classes"
            :key="'cls_'+cls.id"
            :label="cls.class_name+'班'"
            :value="cls.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选项">
        <el-select v-model="form.item" placeholder="请选择">
          <el-option
            v-for="item in items"
            :key="'item_'+item.id"
            :label="item.item_name"
            :value="JSON.stringify(item.children)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选项">
        <el-tree
          :render-after-expand="false"
          @check-change="checkChange"
          :show-checkbox="true"
          :data="outlines"
          :props="{label:'outline_name'}"
        ></el-tree>
        <el-form-item>
          <el-button type="primary" @click="add">添加</el-button>
        </el-form-item>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      form: {
        title: "",
        class_id: "",
        item: "",
        content: []
      },
      classes: [],
      items: [],
      outlines: []
    };
  },
  created() {
    this.getClasses();
    this.getItems();
    this.getOutlines();
  },
  methods: {
    add() {
      this.axios
        .post("/papers/add", {
          title: this.form.title,
          class_id: this.form.class_id,
          item: this.form.item,
          content: JSON.stringify(this.form.content)
        })
        .then(() => {
          this.$router.push({ name: "papers" });
        });
    },
    checkChange(node, flag) {
      if (node.children.length == 0) {
        if (flag) {
          this.form.content.push(node);
        } else {
          let i = this.form.content.indexOf(node);
          this.form.content.splice(i, 1);
        }
      }
    },
    //大纲
    getOutlines() {
      this.axios.get("/outlines").then(ret => {
        this.outlines = ret.data.data;
      });
    },
    //选项
    getItems() {
      this.axios.get("/items").then(ret => {
        this.items = ret.data.data;
      });
    },
    //班级
    getClasses() {
      this.axios
        .get("/classes", {
          params: {
            currentPage: 1,
            pageSize: 9999
          }
        })
        .then(ret => {
          this.classes = ret.data.data.rows;
        });
    }
  }
};
</script>

<style>
.el-tree-node__content {
  border: 1px solid rgba(0, 0, 0, 0.2);
  margin: 5px 0px;
  padding: 10px 0;
}
.add {
  color: #fff;
  text-decoration: none;
}
</style>
